﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using GenerateDiagram;
using System.Reflection;


namespace ToolGenerateDiagram
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        DatabaseExplorer dbExplorer = new DatabaseExplorer();
        DatabasePropertites propertities = new DatabasePropertites();
        private void MainForm_Load(object sender, EventArgs e)
        {
            dbExplorer.Show(this.mainPanel);
            HandleToolstrip(this.mainPanel.DocumentsCount>0);
            CheckForIllegalCrossThreadCalls = false;
        }

        private void databaseExplorerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dbExplorer.DockPanel == null)
                dbExplorer = new DatabaseExplorer();
            dbExplorer.Show(this.mainPanel);
        }

        private void databasePropertiiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var contain= mainPanel.Contents.Contains(propertities);
            if (propertities.PanelPane == null )
                propertities = new DatabasePropertites();
            if (dbExplorer.treeView1.SelectedNode.Tag is AbstractDatabase)
            {
                propertities.Width = 280;
             
                PropertyGrid propertity = new PropertyGrid();
                propertity.SelectedObject = dbExplorer.treeView1.SelectedNode.Tag as AbstractDatabase;
                propertity.Dock = DockStyle.Fill;
                propertities.Controls.Clear();
                propertities.Controls.Add(propertity);
                propertities.DockAreas = WeifenLuo.WinFormsUI.Docking.DockAreas.DockRight;
                TreeNode node = dbExplorer.treeView1.SelectedNode;
                propertities.Text = node.Text + " Propertites";
                if(!contain)
                propertities.Show(dbExplorer.DockPanel);
            }
        }

        private void tsbConnect_Click(object sender, EventArgs e)
        {
            ConnectTo conectt = new ConnectTo();
            conectt.ConnectFail += new EventHandler(conectt_ConnectFail);
            conectt.ConnectSuccessful += new EventHandler(conectt_ConnectSuccessful);
            conectt.ShowDialog();
        }

        void conectt_ConnectSuccessful(object sender, EventArgs e)
        {
            dbExplorer.LoadDatabase(Convert.ToString(sender));
        }

        void conectt_ConnectFail(object sender, EventArgs e)
        {
            dbExplorer.Clear();
            MessageBox.Show("connect fail");
        }

        private void tsbGenCode_Click(object sender, EventArgs e)
        {

        }


        private void CloseAllDocument()
        {
            if (this.mainPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                foreach (var item in base.MdiChildren)
                {
                    item.Close();
                }
            }
            else
            {
                foreach (var doc in mainPanel.DocumentsToArray())
                {
                    doc.DockHandler.Close();
                }
            }
        }

       

        private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.CloseAllDocument();
            HandleToolstrip(this.mainPanel.DocumentsCount > 0); ;
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.mainPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                base.ActiveMdiChild.Close();
            }
            else
            {
                mainPanel.ActiveDocument.DockHandler.Close();
            }
            HandleToolstrip(this.mainPanel.DocumentsCount > 0);
        }

        private void closeAllButThisOneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.mainPanel.DocumentStyle == DocumentStyle.SystemMdi)
            {
                var activeForm = base.ActiveMdiChild;
                foreach (var item in base.MdiChildren)
                {
                    if (item != activeForm)
                        item.Close();
                }
            }
            else
            {
                foreach (var content in mainPanel.DocumentsToArray())
                {
                    if (!content.DockHandler.IsActivated)
                        content.DockHandler.Close();
                }
            }
            HandleToolstrip(this.mainPanel.DocumentsCount > 0);
        }

        private void HandleMenuItem()
        {
            closeAllButThisOneToolStripMenuItem.Enabled = closeToolStripMenuItem.Enabled = closeAllToolStripMenuItem.Enabled = saveAllFileToolStripMenuItem.Enabled = mainPanel.DocumentsCount > 0;
        }

        private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            HandleMenuItem();
        }

        private void mnViewtoolToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            databasePropertiiesToolStripMenuItem.Enabled = dbExplorer.treeView1.SelectedNode.Tag is AbstractDatabase;
        }

        private void saveAllFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var doc in this.mainPanel.DocumentsToArray())
                {
                    var disClass = doc as DisplayClass;
                    disClass.WriteFile(folderBrowserDialog1.SelectedPath);
                }
                MessageBox.Show("Write file completed");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var doc = this.mainPanel.ActiveDocument as DisplayClass;
                doc.WriteFile(folderBrowserDialog1.SelectedPath);
            }
        }

        public void HandleToolstrip(bool enable)
        {
            tsCloseDoc.Enabled = tsSaveAllDoc.Enabled = tsSaveDoc.Enabled = enable;
        }

        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }

    }
}
